home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / mouse.el.z / mouse.el
Encoding:
Text File  |  1998-10-28  |  68.2 KB  |  1,819 lines

  1. ;;; mouse.el --- window system-independent mouse support
  2.  
  3. ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: hardware
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; This package provides various useful commands (including help
  28. ;; system access) through the mouse.  All this code assumes that mouse
  29. ;; interpretation has been abstracted into Emacs input events.
  30. ;;
  31. ;; The code is rather X-dependent.
  32.  
  33. ;;; Code:
  34.  
  35. ;;; Utility functions.
  36.  
  37. ;;; Indent track-mouse like progn.
  38. (put 'track-mouse 'lisp-indent-function 0)
  39.  
  40. (defvar mouse-yank-at-point nil
  41.   "*If non-nil, mouse yank commands yank at point instead of at click.")
  42.  
  43. ;; Provide a mode-specific menu on a mouse button.
  44.  
  45. (defun mouse-major-mode-menu (event)
  46.   "Pop up a mode-specific menu of mouse commands."
  47.   ;; Switch to the window clicked on, because otherwise
  48.   ;; the mode's commands may not make sense.
  49.   (interactive "@e")
  50.   (let (;; This is where mouse-major-mode-menu-prefix
  51.     ;; returns the prefix we should use (after menu-bar).
  52.     ;; It is either nil or (SOME-SYMBOL).
  53.     (mouse-major-mode-menu-prefix nil)
  54.     ;; Make a keymap in which our last command leads to a menu
  55.     (newmap (make-sparse-keymap (concat mode-name " Mode")))
  56.     result)
  57.     ;; Make our menu inherit from the desired keymap
  58.     ;; which we want to display as the menu now.
  59.     (set-keymap-parent newmap
  60.                (mouse-major-mode-menu-1
  61.             (and (current-local-map)
  62.                  (lookup-key (current-local-map) [menu-bar]))))
  63.     (setq result (x-popup-menu t (list newmap)))
  64.     (if result
  65.     (let ((command (key-binding
  66.             (apply 'vector (append '(menu-bar)
  67.                            mouse-major-mode-menu-prefix
  68.                            result)))))
  69.       (if command
  70.           (command-execute command))))))
  71.  
  72. ;; Compute and cache the equivalent keys in MENU and all its submenus.
  73. ;;;(defun mouse-major-mode-menu-compute-equiv-keys (menu)
  74. ;;;  (and (eq (car menu) 'keymap)
  75. ;;;       (x-popup-menu nil menu))
  76. ;;;  (while menu
  77. ;;;    (and (consp (car menu))
  78. ;;;     (consp (cdr (car menu)))
  79. ;;;     (let ((tail (cdr (car menu))))
  80. ;;;       (while (and (consp tail)
  81. ;;;               (not (eq (car tail) 'keymap)))
  82. ;;;         (setq tail (cdr tail)))
  83. ;;;       (if (consp tail)
  84. ;;;           (mouse-major-mode-menu-compute-equiv-keys tail))))
  85. ;;;    (setq menu (cdr menu))))
  86.  
  87. ;; Given a mode's menu bar keymap,
  88. ;; if it defines exactly one menu bar menu,
  89. ;; return just that menu.
  90. ;; Otherwise return a menu for all of them.
  91. (defun mouse-major-mode-menu-1 (menubar)
  92.   (if menubar
  93.       (let ((tail menubar)
  94.         submap)
  95.     (while tail
  96.       (if (consp (car tail))
  97.           (if submap
  98.           (setq submap t)
  99.         (setq submap (car tail))))
  100.       (setq tail (cdr tail)))
  101.     (if (eq submap t)
  102.         menubar
  103.       (setq mouse-major-mode-menu-prefix (list (car submap)))
  104.       (cdr (cdr submap))))))
  105.  
  106. ;; Commands that operate on windows.
  107.  
  108. (defun mouse-minibuffer-check (event)
  109.   (let ((w (posn-window (event-start event))))
  110.     (and (window-minibuffer-p w)
  111.      (not (minibuffer-window-active-p w))
  112.      (error "Minibuffer window is not active")))
  113.   ;; Give temporary modes such as isearch a chance to turn off.
  114.   (run-hooks 'mouse-leave-buffer-hook))
  115.  
  116. (defun mouse-delete-window (click)
  117.   "Delete the window you click on.
  118. This must be bound to a mouse click."
  119.   (interactive "e")
  120.   (mouse-minibuffer-check click)
  121.   (delete-window (posn-window (event-start click))))
  122.  
  123. (defun mouse-select-window (click)
  124.   "Select the window clicked on; don't move point."
  125.   (interactive "e")
  126.   (mouse-minibuffer-check click)
  127.   (let ((oframe (selected-frame))
  128.     (frame (window-frame (posn-window (event-start click)))))
  129.     (select-window (posn-window (event-start click)))
  130.     (raise-frame frame)
  131.     (select-frame frame)
  132.     (or (eq frame oframe)
  133.     (set-mouse-position (selected-frame) (1- (frame-width)) 0))
  134.     (unfocus-frame)))
  135.  
  136. (defun mouse-tear-off-window (click)
  137.   "Delete the window clicked on, and create a new frame displaying its buffer."
  138.   (interactive "e")
  139.   (mouse-minibuffer-check click)
  140.   (let* ((window (posn-window (event-start click)))
  141.      (buf (window-buffer window))
  142.      (frame (make-frame)))
  143.     (select-frame frame)
  144.     (switch-to-buffer buf)
  145.     (delete-window window)))
  146.  
  147. (defun mouse-delete-other-windows ()
  148.   "Delete all window except the one you click on."
  149.   (interactive "@")
  150.   (delete-other-windows))
  151.  
  152. (defun mouse-split-window-vertically (click)
  153.   "Select Emacs window mouse is on, then split it vertically in half.
  154. The window is split at the line clicked on.
  155. This command must be bound to a mouse click."
  156.   (interactive "@e")
  157.   (mouse-minibuffer-check click)
  158.   (let ((start (event-start click)))
  159.     (select-window (posn-window start))
  160.     (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
  161.       (first-line window-min-height)
  162.       (last-line (- (window-height) window-min-height)))
  163.       (if (< last-line first-line)
  164.       (error "Window too short to split")
  165.     (split-window-vertically
  166.      (min (max new-height first-line) last-line))))))
  167.  
  168. (defun mouse-split-window-horizontally (click)
  169.   "Select Emacs window mouse is on, then split it horizontally in half.
  170. The window is split at the column clicked on.
  171. This command must be bound to a mouse click."
  172.   (interactive "@e")
  173.   (mouse-minibuffer-check click)
  174.   (let ((start (event-start click)))
  175.     (select-window (posn-window start))
  176.     (let ((new-width (1+ (car (posn-col-row (event-end click)))))
  177.       (first-col window-min-width)
  178.       (last-col (- (window-width) window-min-width)))
  179.       (if (< last-col first-col)
  180.       (error "Window too narrow to split")
  181.     (split-window-horizontally
  182.      (min (max new-width first-col) last-col))))))
  183.  
  184. (defun mouse-drag-mode-line (start-event)
  185.   "Change the height of a window by dragging on the mode line."
  186.   (interactive "e")
  187.   ;; Give temporary modes such as isearch a chance to turn off.
  188.   (run-hooks 'mouse-leave-buffer-hook)
  189.   (let ((done nil)
  190.     (echo-keystrokes 0)
  191.     (start-event-frame (window-frame (car (car (cdr start-event)))))
  192.     (start-event-window (car (car (cdr start-event))))
  193.     (start-nwindows (count-windows t))
  194.     (old-selected-window (selected-window))
  195.     should-enlarge-minibuffer
  196.     event mouse minibuffer y top bot edges wconfig params growth)
  197.     (setq params (frame-parameters))
  198.     (if (and (not (setq minibuffer (cdr (assq 'minibuffer params))))
  199.          (one-window-p t))
  200.     (error "Attempt to resize sole window"))
  201.     (track-mouse
  202.       (progn
  203.     ;; enlarge-window only works on the selected window, so
  204.     ;; we must select the window where the start event originated.
  205.     ;; unwind-protect will restore the old selected window later.
  206.     (select-window start-event-window)
  207.     ;; if this is the bottommost ordinary window, then to
  208.     ;; move its modeline the minibuffer must be enlarged.
  209.     (setq should-enlarge-minibuffer
  210.           (and minibuffer
  211.            (not (one-window-p t))
  212.            (= (nth 1 (window-edges minibuffer))
  213.               (nth 3 (window-edges)))))
  214.     ;; loop reading events and sampling the position of
  215.     ;; the mouse.
  216.     (while (not done)
  217.       (setq event (read-event)
  218.         mouse (mouse-position))
  219.       ;; do nothing if
  220.       ;;   - there is a switch-frame event.
  221.       ;;   - the mouse isn't in the frame that we started in
  222.       ;;   - the mouse isn't in any Emacs frame
  223.       ;; drag if
  224.       ;;   - there is a mouse-movement event
  225.       ;;   - there is a scroll-bar-movement event
  226.       ;;     (same as mouse movement for our purposes)
  227.       ;; quit if
  228.       ;;   - there is a keyboard event or some other unknown event
  229.       ;;     unknown event.
  230.       (cond ((integerp event)
  231.          (setq done t))
  232.         ((eq (car event) 'switch-frame)
  233.          nil)
  234.         ((not (memq (car event)
  235.                 '(mouse-movement scroll-bar-movement)))
  236.          (if (consp event)
  237.              (setq unread-command-events
  238.                (cons event unread-command-events)))
  239.          (setq done t))
  240.         ((not (eq (car mouse) start-event-frame))
  241.          nil)
  242.         ((null (car (cdr mouse)))
  243.          nil)
  244.         (t
  245.          (setq y (cdr (cdr mouse))
  246.                edges (window-edges)
  247.                top (nth 1 edges)
  248.                bot (nth 3 edges))
  249.          ;; scale back a move that would make the
  250.          ;; window too short.
  251.          (cond ((< (- y top -1) window-min-height)
  252.             (setq y (+ top window-min-height -1))))
  253.          ;; compute size change needed
  254.          (setq growth (- y bot -1)
  255.                wconfig (current-window-configuration))
  256.          ;; grow/shrink minibuffer?
  257.          (if should-enlarge-minibuffer
  258.              (progn
  259.                ;; yes.  briefly select minibuffer so
  260.                ;; enlarge-window will affect the
  261.                ;; correct window.
  262.                (select-window minibuffer)
  263.                ;; scale back shrinkage if it would
  264.                ;; make the minibuffer less than 1
  265.                ;; line tall.
  266.                (if (and (> growth 0)
  267.                 (< (- (window-height minibuffer)
  268.                       growth)
  269.                    1))
  270.                (setq growth (1- (window-height minibuffer))))
  271.                (enlarge-window (- growth))
  272.                (select-window start-event-window))
  273.            ;; no.  grow/shrink the selected window
  274.            (enlarge-window growth))
  275.          ;; if this window's growth caused another
  276.          ;; window to be deleted because it was too
  277.          ;; short, rescind the change.
  278.          ;;
  279.          ;; if size change caused space to be stolen
  280.          ;; from a window above this one, rescind the
  281.          ;; change, but only if we didn't grow/srhink
  282.          ;; the minibuffer.  minibuffer size changes
  283.          ;; can cause all windows to shrink... no way
  284.          ;; around it.
  285.          (if (or (/= start-nwindows (count-windows t))
  286.              (and (not should-enlarge-minibuffer)
  287.                   (/= top (nth 1 (window-edges)))))
  288.              (set-window-configuration wconfig)))))))))
  289.  
  290. (defun mouse-drag-vertical-line (start-event)
  291.   "Change the width of a window by dragging on the vertical line."
  292.   (interactive "e")
  293.   ;; Give temporary modes such as isearch a chance to turn off.
  294.   (run-hooks 'mouse-leave-buffer-hook)
  295.   (let ((done nil)
  296.     (echo-keystrokes 0)
  297.     (start-event-frame (window-frame (car (car (cdr start-event)))))
  298.     (start-event-window (car (car (cdr start-event))))
  299.     (start-nwindows (count-windows t))
  300.     (old-selected-window (selected-window))
  301.     event mouse x left right edges wconfig growth)
  302.     (if (one-window-p t)
  303.     (error "Attempt to resize sole ordinary window"))
  304.     (if (= (nth 2 (window-edges start-event-window))
  305.        (frame-width start-event-frame))
  306.     (error "Attempt to drag rightmost scrollbar"))
  307.     (track-mouse
  308.       (progn
  309.     ;; enlarge-window only works on the selected window, so
  310.     ;; we must select the window where the start event originated.
  311.     ;; unwind-protect will restore the old selected window later.
  312.     (select-window start-event-window)
  313.     ;; loop reading events and sampling the position of
  314.     ;; the mouse.
  315.     (while (not done)
  316.       (setq event (read-event)
  317.         mouse (mouse-position))
  318.       ;; do nothing if
  319.       ;;   - there is a switch-frame event.
  320.       ;;   - the mouse isn't in the frame that we started in
  321.       ;;   - the mouse isn't in any Emacs frame
  322.       ;; drag if
  323.       ;;   - there is a mouse-movement event
  324.       ;;   - there is a scroll-bar-movement event
  325.       ;;     (same as mouse movement for our purposes)
  326.       ;; quit if
  327.       ;;   - there is a keyboard event or some other unknown event
  328.       ;;     unknown event.
  329.       (cond ((integerp event)
  330.          (setq done t))
  331.         ((eq (car event) 'switch-frame)
  332.          nil)
  333.         ((not (memq (car event)
  334.                 '(mouse-movement scroll-bar-movement)))
  335.          (if (consp event)
  336.              (setq unread-command-events
  337.                (cons event unread-command-events)))
  338.          (setq done t))
  339.         ((not (eq (car mouse) start-event-frame))
  340.          nil)
  341.         ((null (car (cdr mouse)))
  342.          nil)
  343.         (t
  344.          (setq x (car (cdr mouse))
  345.                edges (window-edges)
  346.                left (nth 0 edges)
  347.                right (nth 2 edges))
  348.          ;; scale back a move that would make the
  349.          ;; window too thin.
  350.          (cond ((< (- x left -1) window-min-width)
  351.             (setq x (+ left window-min-width -1))))
  352.          ;; compute size change needed
  353.          (setq growth (- x right -1)
  354.                wconfig (current-window-configuration))
  355.          (enlarge-window growth t)
  356.          ;; if this window's growth caused another
  357.          ;; window to be deleted because it was too
  358.          ;; thin, rescind the change.
  359.          ;;
  360.          ;; if size change caused space to be stolen
  361.          ;; from a window to the left of this one,
  362.          ;; rescind the change.
  363.          (if (or (/= start-nwindows (count-windows t))
  364.              (/= left (nth 0 (window-edges))))
  365.              (set-window-configuration wconfig)))))))))
  366.  
  367. (defun mouse-set-point (event)
  368.   "Move point to the position clicked on with the mouse.
  369. This should be bound to a mouse click event type."
  370.   (interactive "e")
  371.   (mouse-minibuffer-check event)
  372.   ;; Use event-end in case called from mouse-drag-region.
  373.   ;; If EVENT is a click, event-end and event-start give same value.
  374.   (let ((posn (event-end event)))
  375.     (if (not (windowp (posn-window posn)))
  376.     (error "Cursor not in text area of window"))
  377.     (select-window (posn-window posn))
  378.     (if (numberp (posn-point posn))
  379.     (goto-char (posn-point posn)))))
  380.  
  381. (defvar mouse-last-region-beg nil)
  382. (defvar mouse-last-region-end nil)
  383. (defvar mouse-last-region-tick nil)
  384.  
  385. (defun mouse-region-match ()
  386.   "Return non-nil if there's an active region that was set with the mouse."
  387.   (and (mark t) mark-active
  388.        (eq mouse-last-region-beg (region-beginning))
  389.        (eq mouse-last-region-end (region-end))
  390.        (eq mouse-last-region-tick (buffer-modified-tick))))
  391.  
  392. (defun mouse-set-region (click)
  393.   "Set the region to the text dragged over, and copy to kill ring.
  394. This should be bound to a mouse drag event."
  395.   (interactive "e")
  396.   (mouse-minibuffer-check click)
  397.   (let ((posn (event-start click))
  398.     (end (event-end click)))
  399.     (select-window (posn-window posn))
  400.     (if (numberp (posn-point posn))
  401.     (goto-char (posn-point posn)))
  402.     ;; If mark is highlighted, no need to bounce the cursor.
  403.     ;; On X, we highlight while dragging, thus once again no need to bounce.
  404.     (or transient-mark-mode
  405.     (eq (framep (selected-frame)) 'x)
  406.     (eq (framep (selected-frame)) 'pc)
  407.     (eq (framep (selected-frame)) 'win32)
  408.     (sit-for 1))
  409.     (push-mark)
  410.     (set-mark (point))
  411.     (if (numberp (posn-point end))
  412.     (goto-char (posn-point end)))
  413.     ;; Don't set this-command to kill-region, so that a following
  414.     ;; C-w will not double the text in the kill ring.
  415.     ;; Ignore last-command so we don't append to a preceding kill.
  416.     (let (this-command last-command)
  417.       (copy-region-as-kill (mark) (point)))
  418.     (mouse-set-region-1)))
  419.  
  420. (defun mouse-set-region-1 ()
  421.   (setq mouse-last-region-beg (region-beginning))
  422.   (setq mouse-last-region-end (region-end))
  423.   (setq mouse-last-region-tick (buffer-modified-tick)))
  424.  
  425. (defvar mouse-scroll-delay 0.25
  426.   "*The pause between scroll steps caused by mouse drags, in seconds.
  427. If you drag the mouse beyond the edge of a window, Emacs scrolls the
  428. window to bring the text beyond that edge into view, with a delay of
  429. this many seconds between scroll steps.  Scrolling stops when you move
  430. the mouse back into the window, or release the button.
  431. This variable's value may be non-integral.
  432. Setting this to zero causes Emacs to scroll as fast as it can.")
  433.  
  434. (defvar mouse-scroll-min-lines 1
  435.   "*The minimum number of lines scrolled by dragging mouse out of window.
  436. Moving the mouse out the top or bottom edge of the window begins
  437. scrolling repeatedly.  The number of lines scrolled per repetition
  438. is normally equal to the number of lines beyond the window edge that
  439. the mouse has moved.  However, it always scrolls at least the number
  440. of lines specified by this variable.")
  441.  
  442. (defun mouse-scroll-subr (window jump &optional overlay start)
  443.   "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
  444. If OVERLAY is an overlay, let it stretch from START to the far edge of
  445. the newly visible text.
  446. Upon exit, point is at the far edge of the newly visible text."
  447.   (cond
  448.    ((and (> jump 0) (< jump mouse-scroll-min-lines))
  449.     (setq jump mouse-scroll-min-lines))
  450.    ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
  451.     (setq jump (- mouse-scroll-min-lines))))
  452.   (let ((opoint (point)))
  453.     (while (progn
  454.          (goto-char (window-start window))
  455.          (if (not (zerop (vertical-motion jump window)))
  456.          (progn
  457.            (set-window-start window (point))
  458.            (if (natnump jump)
  459.                (progn
  460.              (goto-char (window-end window))
  461.              ;; window-end doesn't reflect the window's new
  462.              ;; start position until the next redisplay.  Hurrah.
  463.              (vertical-motion (1- jump) window))
  464.              (goto-char (window-start window)))
  465.            (if overlay
  466.                (move-overlay overlay start (point)))
  467.            ;; Now that we have scrolled WINDOW properly,
  468.            ;; put point back where it was for the redisplay
  469.            ;; so that we don't mess up the selected window.
  470.            (or (eq window (selected-window))
  471.                (goto-char opoint))
  472.            (sit-for mouse-scroll-delay)))))
  473.     (or (eq window (selected-window))
  474.     (goto-char opoint))))
  475.  
  476. ;; Create an overlay and immediately delete it, to get "overlay in no buffer".
  477. (defvar mouse-drag-overlay (make-overlay 1 1))
  478. (delete-overlay mouse-drag-overlay)
  479. (overlay-put mouse-drag-overlay 'face 'region)
  480.  
  481. (defvar mouse-selection-click-count 0)
  482.  
  483. (defvar mouse-selection-click-count-buffer nil)
  484.  
  485. (defun mouse-drag-region (start-event)
  486.   "Set the region to the text that the mouse is dragged over.
  487. Highlight the drag area as you move the mouse.
  488. This must be bound to a button-down mouse event.
  489. In Transient Mark mode, the highlighting remains as long as the mark
  490. remains active.  Otherwise, it remains until the next input event."
  491.   (interactive "e")
  492.   (mouse-minibuffer-check start-event)
  493.   (let* ((echo-keystrokes 0)
  494.      (start-posn (event-start start-event))
  495.      (start-point (posn-point start-posn))
  496.      (start-window (posn-window start-posn))
  497.      (start-frame (window-frame start-window))
  498.      (bounds (window-edges start-window))
  499.      (top (nth 1 bounds))
  500.      (bottom (if (window-minibuffer-p start-window)
  501.              (nth 3 bounds)
  502.            ;; Don't count the mode line.
  503.            (1- (nth 3 bounds))))
  504.      (click-count (1- (event-click-count start-event))))
  505.     (setq mouse-selection-click-count click-count)
  506.     (setq mouse-selection-click-count-buffer (current-buffer))
  507.     (mouse-set-point start-event)
  508.     ;; In case the down click is in the middle of some intangible text,
  509.     ;; use the end of that text, and put it in START-POINT.
  510.     (if (< (point) start-point)
  511.     (goto-char start-point))
  512.     (setq start-point (point))
  513.     (let ((range (mouse-start-end start-point start-point click-count)))
  514.       (move-overlay mouse-drag-overlay (car range) (nth 1 range)
  515.             (window-buffer start-window)))
  516.     (deactivate-mark)
  517.     ;; end-of-range is used only in the single-click case.
  518.     ;; It is the place where the drag has reached so far
  519.     ;; (but not outside the window where the drag started).
  520.     (let (event end end-point last-end-point (end-of-range (point)))
  521.       (track-mouse
  522.     (while (progn
  523.          (setq event (read-event))
  524.          (or (mouse-movement-p event)
  525.              (eq (car-safe event) 'switch-frame)))
  526.       (if (eq (car-safe event) 'switch-frame)
  527.           nil
  528.         (setq end (event-end event)
  529.           end-point (posn-point end))
  530.         (if end-point
  531.         (setq last-end-point end-point))
  532.  
  533.         (cond
  534.          ;; Are we moving within the original window?
  535.          ((and (eq (posn-window end) start-window)
  536.            (integer-or-marker-p end-point))
  537.           ;; Go to START-POINT first, so that when we move to END-POINT,
  538.           ;; if it's in the middle of intangible text,
  539.           ;; point jumps in the direction away from START-POINT.
  540.           (goto-char start-point)
  541.           (goto-char end-point)
  542.           (if (zerop (% click-count 3))
  543.           (setq end-of-range (point)))
  544.           (let ((range (mouse-start-end start-point (point) click-count)))
  545.         (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
  546.  
  547.          (t
  548.           (let ((mouse-row (cdr (cdr (mouse-position)))))
  549.         (cond
  550.          ((null mouse-row))
  551.          ((< mouse-row top)
  552.           (mouse-scroll-subr start-window (- mouse-row top)
  553.                      mouse-drag-overlay start-point)
  554.           ;; Without this, point tends to jump back to the starting
  555.           ;; position where the mouse button was pressed down.
  556.           (setq end-of-range (overlay-start mouse-drag-overlay)))
  557.          ((>= mouse-row bottom)
  558.           (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
  559.                      mouse-drag-overlay start-point)
  560.           (setq end-of-range (overlay-end mouse-drag-overlay))))))))))
  561.       (if (consp event)
  562.       (let ((fun (key-binding (vector (car event)))))
  563.         ;; Run the binding of the terminating up-event, if possible.
  564.         ;; In the case of a multiple click, it gives the wrong results,
  565.         ;; because it would fail to set up a region.
  566.         (if nil ;; (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
  567.         ;; In this case, we can just let the up-event execute normally.
  568.         (let ((end (event-end event)))
  569.           ;; Set the position in the event before we replay it,
  570.           ;; because otherwise it may have a position in the wrong
  571.           ;; buffer.
  572.           (setcar (cdr end) end-of-range)
  573.           ;; Delete the overlay before calling the function,
  574.           ;; because delete-overlay increases buffer-modified-tick.
  575.           (delete-overlay mouse-drag-overlay)
  576.           (setq unread-command-events
  577.             (cons event unread-command-events)))
  578.           (if (not (= (overlay-start mouse-drag-overlay)
  579.               (overlay-end mouse-drag-overlay)))
  580.           (let* ((stop-point (or (posn-point (event-end event)) last-end-point))
  581.              ;; The end that comes from where we ended the drag.
  582.              ;; Point goes here.
  583.              (region-termination
  584.               (if (and stop-point (< stop-point start-point))
  585.                   (overlay-start mouse-drag-overlay)
  586.                 (overlay-end mouse-drag-overlay)))
  587.              ;; The end that comes from where we started the drag.
  588.              ;; Mark goes there.
  589.              (region-commencement
  590.               (- (+ (overlay-end mouse-drag-overlay)
  591.                 (overlay-start mouse-drag-overlay))
  592.                  region-termination))
  593.              last-command this-command)
  594.             (push-mark region-commencement t t)
  595.             (goto-char region-termination)
  596.             (copy-region-as-kill (point) (mark t))
  597.             (let ((buffer (current-buffer)))
  598.               (mouse-show-mark)
  599.               ;; mouse-show-mark can call read-event,
  600.               ;; and that means the Emacs server could switch buffers
  601.               ;; under us.  If that happened, 
  602.               ;; avoid trying to use the region.
  603.               (and (mark t) mark-active
  604.                (eq buffer (current-buffer))
  605.                (mouse-set-region-1))))
  606.         (goto-char (overlay-end mouse-drag-overlay))
  607.         (setq this-command 'mouse-set-point)
  608.         (delete-overlay mouse-drag-overlay))))
  609.     (delete-overlay mouse-drag-overlay)))))
  610.  
  611. ;; Commands to handle xterm-style multiple clicks.
  612.  
  613. (defun mouse-skip-word (dir)
  614.   "Skip over word, over whitespace, or over identical punctuation.
  615. If DIR is positive skip forward; if negative, skip backward."
  616.   (let* ((char (following-char))
  617.      (syntax (char-to-string (char-syntax char))))
  618.     (cond ((or (string= syntax "w") (string= syntax " "))
  619.        (if (< dir 0)
  620.            (skip-syntax-backward syntax)
  621.          (skip-syntax-forward syntax)))
  622.       ((string= syntax "_")
  623.        (if (< dir 0)
  624.            (skip-syntax-backward "w_")
  625.          (skip-syntax-forward "w_")))
  626.       ((< dir 0)
  627.        (while (and (not (bobp)) (= (preceding-char) char))
  628.          (forward-char -1)))
  629.       (t
  630.        (while (and (not (eobp)) (= (following-char) char))
  631.          (forward-char 1))))))
  632.  
  633. ;; Return a list of region bounds based on START and END according to MODE.
  634. ;; If MODE is 0 then set point to (min START END), mark to (max START END).
  635. ;; If MODE is 1 then set point to start of word at (min START END),
  636. ;; mark to end of word at (max START END).
  637. ;; If MODE is 2 then do the same for lines.
  638. (defun mouse-start-end (start end mode)
  639.   (if (> start end)
  640.       (let ((temp start))
  641.         (setq start end
  642.               end temp)))
  643.   (setq mode (mod mode 3))
  644.   (cond ((= mode 0)
  645.      (list start end))
  646.         ((and (= mode 1)
  647.               (= start end)
  648.           (char-after start)
  649.               (= (char-syntax (char-after start)) ?\())
  650.      (list start
  651.            (save-excursion
  652.          (goto-char start)
  653.          (forward-sexp 1)
  654.          (point))))
  655.         ((and (= mode 1)
  656.               (= start end)
  657.           (char-after start)
  658.               (= (char-syntax (char-after start)) ?\)))
  659.      (list (save-excursion 
  660.          (goto-char (1+ start))
  661.          (backward-sexp 1)
  662.          (point))
  663.            (1+ start)))
  664.     ((and (= mode 1)
  665.               (= start end)
  666.           (char-after start)
  667.               (= (char-syntax (char-after start)) ?\"))
  668.      (let ((open (or (eq start (point-min))
  669.              (save-excursion
  670.                (goto-char (- start 1))
  671.                (looking-at "\\s(\\|\\s \\|\\s>")))))
  672.        (if open
  673.            (list start
  674.              (save-excursion
  675.                (condition-case nil
  676.                (progn 
  677.                  (goto-char start)
  678.                  (forward-sexp 1)
  679.                  (point))
  680.              (error end))))
  681.          (list (1+ start)
  682.            (save-excursion
  683.              (condition-case nil
  684.              (progn
  685.                (goto-char (1+ start))
  686.                (backward-sexp 1)
  687.                (point))
  688.                (error end)))))))
  689.         ((= mode 1)
  690.      (list (save-excursion
  691.          (goto-char start)
  692.          (mouse-skip-word -1)
  693.          (point))
  694.            (save-excursion
  695.          (goto-char end)
  696.          (mouse-skip-word 1)
  697.          (point))))
  698.         ((= mode 2)
  699.      (list (save-excursion
  700.          (goto-char start)
  701.          (beginning-of-line 1)
  702.          (point))
  703.            (save-excursion
  704.          (goto-char end)
  705.          (forward-line 1)
  706.          (point))))))
  707.  
  708. ;; Subroutine: set the mark where CLICK happened,
  709. ;; but don't do anything else.
  710. (defun mouse-set-mark-fast (click)
  711.   (mouse-minibuffer-check click)
  712.   (let ((posn (event-start click)))
  713.     (select-window (posn-window posn))
  714.     (if (numberp (posn-point posn))
  715.     (push-mark (posn-point posn) t t))))
  716.  
  717. (defun mouse-undouble-last-event (events)
  718.   (let* ((index (1- (length events)))
  719.      (last (nthcdr index events))
  720.      (event (car last))
  721.      (basic (event-basic-type event))
  722.      (modifiers (delq 'double (delq 'triple (copy-sequence (event-modifiers event)))))
  723.      (new
  724.       (if (consp event)
  725.           (cons (event-convert-list (nreverse (cons basic modifiers)))
  726.             (cdr event))
  727.         event)))
  728.     (setcar last new)
  729.     (if (key-binding (apply 'vector events))
  730.     t
  731.       (setcar last event)
  732.       nil)))
  733.  
  734. ;; Momentarily show where the mark is, if highlighting doesn't show it. 
  735. (defun mouse-show-mark ()
  736.   (if transient-mark-mode
  737.       (if window-system
  738.       (delete-overlay mouse-drag-overlay))
  739.     (if window-system
  740.     (let ((inhibit-quit t)
  741.           (echo-keystrokes 0)
  742.           event events)
  743.       (move-overlay mouse-drag-overlay (point) (mark t))
  744.       (while (progn (setq event (read-event))
  745.             (setq events (append events (list event)))
  746.             (and (memq 'down (event-modifiers event))
  747.                  (not (key-binding (apply 'vector events)))
  748.                  (not (mouse-undouble-last-event events)))))
  749.       (setq unread-command-events
  750.         (nconc events unread-command-events))
  751.       (setq quit-flag nil)
  752.       (delete-overlay mouse-drag-overlay))
  753.       (save-excursion
  754.        (goto-char (mark t))
  755.        (sit-for 1)))))
  756.  
  757. (defun mouse-set-mark (click)
  758.   "Set mark at the position clicked on with the mouse.
  759. Display cursor at that position for a second.
  760. This must be bound to a mouse click."
  761.   (interactive "e")
  762.   (mouse-minibuffer-check click)
  763.   (select-window (posn-window (event-start click)))
  764.   ;; We don't use save-excursion because that preserves the mark too.
  765.   (let ((point-save (point)))
  766.     (unwind-protect
  767.     (progn (mouse-set-point click)
  768.            (push-mark nil t t)
  769.            (or transient-mark-mode
  770.            (sit-for 1)))
  771.       (goto-char point-save))))
  772.  
  773. (defun mouse-kill (click)
  774.   "Kill the region between point and the mouse click.
  775. The text is saved in the kill ring, as with \\[kill-region]."
  776.   (interactive "e")
  777.   (mouse-minibuffer-check click)
  778.   (let* ((posn (event-start click))
  779.      (click-posn (posn-point posn)))
  780.     (select-window (posn-window posn))
  781.     (if (numberp click-posn)
  782.     (kill-region (min (point) click-posn)
  783.              (max (point) click-posn)))))
  784.  
  785. (defun mouse-yank-at-click (click arg)
  786.   "Insert the last stretch of killed text at the position clicked on.
  787. Also move point to one end of the text thus inserted (normally the end).
  788. Prefix arguments are interpreted as with \\[yank].
  789. If `mouse-yank-at-point' is non-nil, insert at point
  790. regardless of where you click."
  791.   (interactive "e\nP")
  792.   ;; Give temporary modes such as isearch a chance to turn off.
  793.   (run-hooks 'mouse-leave-buffer-hook)
  794.   (or mouse-yank-at-point (mouse-set-point click))
  795.   (setq this-command 'yank)
  796.   (setq mouse-selection-click-count 0)
  797.   (yank arg))
  798.  
  799. (defun mouse-kill-ring-save (click)
  800.   "Copy the region between point and the mouse click in the kill ring.
  801. This does not delete the region; it acts like \\[kill-ring-save]."
  802.   (interactive "e")
  803.   (mouse-set-mark-fast click)
  804.   (let (this-command last-command)
  805.     (kill-ring-save (point) (mark t)))
  806.   (mouse-show-mark))
  807.  
  808. ;;; This function used to delete the text between point and the mouse
  809. ;;; whenever it was equal to the front of the kill ring, but some
  810. ;;; people found that confusing.
  811.  
  812. ;;; A list (TEXT START END), describing the text and position of the last
  813. ;;; invocation of mouse-save-then-kill.
  814. (defvar mouse-save-then-kill-posn nil)
  815.  
  816. (defun mouse-save-then-kill-delete-region (beg end)
  817.   ;; We must make our own undo boundaries
  818.   ;; because they happen automatically only for the current buffer.
  819.   (undo-boundary)
  820.   (if (or (= beg end) (eq buffer-undo-list t))
  821.       ;; If we have no undo list in this buffer,
  822.       ;; just delete.
  823.       (delete-region beg end)
  824.     ;; Delete, but make the undo-list entry share with the kill ring.
  825.     ;; First, delete just one char, so in case buffer is being modified
  826.     ;; for the first time, the undo list records that fact.
  827.     (let (before-change-function after-change-function
  828.       before-change-functions after-change-functions)
  829.       (delete-region beg
  830.              (+ beg (if (> end beg) 1 -1))))
  831.     (let ((buffer-undo-list buffer-undo-list))
  832.       ;; Undo that deletion--but don't change the undo list!
  833.       (let (before-change-function after-change-function
  834.         before-change-functions after-change-functions)
  835.     (primitive-undo 1 buffer-undo-list))
  836.       ;; Now delete the rest of the specified region,
  837.       ;; but don't record it.
  838.       (setq buffer-undo-list t)
  839.       (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
  840.       (error "Lossage in mouse-save-then-kill-delete-region"))
  841.       (delete-region beg end))
  842.     (let ((tail buffer-undo-list))
  843.       ;; Search back in buffer-undo-list for the string
  844.       ;; that came from deleting one character.
  845.       (while (and tail (not (stringp (car (car tail)))))
  846.     (setq tail (cdr tail)))
  847.       ;; Replace it with an entry for the entire deleted text.
  848.       (and tail
  849.        (setcar tail (cons (car kill-ring) (min beg end))))))
  850.   (undo-boundary))
  851.  
  852. (defun mouse-save-then-kill (click)
  853.   "Save text to point in kill ring; the second time, kill the text.
  854. If the text between point and the mouse is the same as what's
  855. at the front of the kill ring, this deletes the text.
  856. Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
  857. which prepares for a second click to delete the text.
  858.  
  859. If you have selected words or lines, this command extends the
  860. selection through the word or line clicked on.  If you do this
  861. again in a different position, it extends the selection again.
  862. If you do this twice in the same position, the selection is killed." 
  863.   (interactive "e")
  864.   (let ((before-scroll point-before-scroll))
  865.     (mouse-minibuffer-check click)
  866.     (let ((click-posn (posn-point (event-start click)))
  867.       ;; Don't let a subsequent kill command append to this one:
  868.       ;; prevent setting this-command to kill-region.
  869.       (this-command this-command))
  870.       (if (and (save-excursion
  871.          (set-buffer (window-buffer (posn-window (event-start click))))
  872.          (and (mark t) (> (mod mouse-selection-click-count 3) 0)
  873.               ;; Don't be fooled by a recent click in some other buffer.
  874.               (eq mouse-selection-click-count-buffer 
  875.               (current-buffer)))))
  876.       (if (not (and (eq last-command 'mouse-save-then-kill)
  877.             (equal click-posn
  878.                    (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
  879.           ;; Find both ends of the object selected by this click.
  880.           (let* ((range
  881.               (mouse-start-end click-posn click-posn
  882.                        mouse-selection-click-count)))
  883.         ;; Move whichever end is closer to the click.
  884.         ;; That's what xterm does, and it seems reasonable.
  885.         (if (< (abs (- click-posn (mark t)))
  886.                (abs (- click-posn (point))))
  887.             (set-mark (car range))
  888.           (goto-char (nth 1 range)))
  889.         ;; We have already put the old region in the kill ring.
  890.         ;; Replace it with the extended region.
  891.         ;; (It would be annoying to make a separate entry.)
  892.         (kill-new (buffer-substring (point) (mark t)) t)
  893.         (mouse-set-region-1)
  894.         ;; Arrange for a repeated mouse-3 to kill this region.
  895.         (setq mouse-save-then-kill-posn
  896.               (list (car kill-ring) (point) click-posn))
  897.         (mouse-show-mark))
  898.         ;; If we click this button again without moving it,
  899.         ;; that time kill.
  900.         (mouse-save-then-kill-delete-region (mark) (point))
  901.         (setq mouse-selection-click-count 0)
  902.         (setq mouse-save-then-kill-posn nil))
  903.     (if (and (eq last-command 'mouse-save-then-kill)
  904.          mouse-save-then-kill-posn
  905.          (eq (car mouse-save-then-kill-posn) (car kill-ring))
  906.          (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
  907.         ;; If this is the second time we've called
  908.         ;; mouse-save-then-kill, delete the text from the buffer.
  909.         (progn
  910.           (mouse-save-then-kill-delete-region (point) (mark))
  911.           ;; After we kill, another click counts as "the first time".
  912.           (setq mouse-save-then-kill-posn nil))
  913.       ;; This is not a repetition.
  914.       ;; We are adjusting an old selection or creating a new one.
  915.       (if (or (and (eq last-command 'mouse-save-then-kill)
  916.                mouse-save-then-kill-posn)
  917.           (and mark-active transient-mark-mode)
  918.           (and (memq last-command
  919.                  '(mouse-drag-region mouse-set-region))
  920.                (or mark-even-if-inactive
  921.                (not transient-mark-mode))))
  922.           ;; We have a selection or suitable region, so adjust it.
  923.           (let* ((posn (event-start click))
  924.              (new (posn-point posn)))
  925.         (select-window (posn-window posn))
  926.         (if (numberp new)
  927.             (progn
  928.               ;; Move whichever end of the region is closer to the click.
  929.               ;; That is what xterm does, and it seems reasonable.
  930.               (if (< (abs (- new (point))) (abs (- new (mark t))))
  931.               (goto-char new)
  932.             (set-mark new))
  933.               (setq deactivate-mark nil)))
  934.         (kill-new (buffer-substring (point) (mark t)) t)
  935.         (mouse-show-mark))
  936.         ;; Set the mark where point is, then move where clicked.
  937.         (mouse-set-mark-fast click)
  938.         (if before-scroll
  939.         (goto-char before-scroll))
  940.         (exchange-point-and-mark)
  941.         (kill-new (buffer-substring (point) (mark t)))
  942.         (if window-system
  943.         (mouse-show-mark)))
  944.       (mouse-set-region-1)
  945.       (setq mouse-save-then-kill-posn
  946.         (list (car kill-ring) (point) click-posn)))))))
  947.  
  948. (global-set-key [M-mouse-1] 'mouse-start-secondary)
  949. (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
  950. (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
  951. (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
  952. (global-set-key [M-mouse-2] 'mouse-yank-secondary)
  953.  
  954. ;; An overlay which records the current secondary selection
  955. ;; or else is deleted when there is no secondary selection.
  956. ;; May be nil.
  957. (defvar mouse-secondary-overlay nil)
  958.  
  959. (defvar mouse-secondary-click-count 0)
  960.  
  961. ;; A marker which records the specified first end for a secondary selection.
  962. ;; May be nil.
  963. (defvar mouse-secondary-start nil)
  964.  
  965. (defun mouse-start-secondary (click)
  966.   "Set one end of the secondary selection to the position clicked on.
  967. Use \\[mouse-secondary-save-then-kill] to set the other end
  968. and complete the secondary selection."
  969.   (interactive "e")
  970.   (mouse-minibuffer-check click)
  971.   (let ((posn (event-start click)))
  972.     (save-excursion
  973.       (set-buffer (window-buffer (posn-window posn)))
  974.       ;; Cancel any preexisting secondary selection.
  975.       (if mouse-secondary-overlay
  976.       (delete-overlay mouse-secondary-overlay))
  977.       (if (numberp (posn-point posn))
  978.       (progn
  979.         (or mouse-secondary-start
  980.         (setq mouse-secondary-start (make-marker)))
  981.         (move-marker mouse-secondary-start (posn-point posn)))))))
  982.  
  983. (defun mouse-set-secondary (click)
  984.   "Set the secondary selection to the text that the mouse is dragged over.
  985. This must be bound to a mouse drag event."
  986.   (interactive "e")
  987.   (mouse-minibuffer-check click)
  988.   (let ((posn (event-start click))
  989.     beg
  990.     (end (event-end click)))
  991.     (save-excursion
  992.       (set-buffer (window-buffer (posn-window posn)))
  993.       (if (numberp (posn-point posn))
  994.       (setq beg (posn-point posn)))
  995.       (if mouse-secondary-overlay
  996.       (move-overlay mouse-secondary-overlay beg (posn-point end))
  997.     (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
  998.       (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
  999.  
  1000. (defun mouse-drag-secondary (start-event)
  1001.   "Set the secondary selection to the text that the mouse is dragged over.
  1002. Highlight the drag area as you move the mouse.
  1003. This must be bound to a button-down mouse event."
  1004.   (interactive "e")
  1005.   (mouse-minibuffer-check start-event)
  1006.   (let* ((echo-keystrokes 0)
  1007.      (start-posn (event-start start-event))
  1008.      (start-point (posn-point start-posn))
  1009.      (start-window (posn-window start-posn))
  1010.      (start-frame (window-frame start-window))
  1011.      (bounds (window-edges start-window))
  1012.      (top (nth 1 bounds))
  1013.      (bottom (if (window-minibuffer-p start-window)
  1014.              (nth 3 bounds)
  1015.            ;; Don't count the mode line.
  1016.            (1- (nth 3 bounds))))
  1017.      (click-count (1- (event-click-count start-event))))
  1018.     (save-excursion
  1019.       (set-buffer (window-buffer start-window))
  1020.       (setq mouse-secondary-click-count click-count)
  1021.       (or mouse-secondary-overlay
  1022.       (setq mouse-secondary-overlay
  1023.         (make-overlay (point) (point))))
  1024.       (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
  1025.       (if (> (mod click-count 3) 0)
  1026.       ;; Double or triple press: make an initial selection
  1027.       ;; of one word or line.
  1028.       (let ((range (mouse-start-end start-point start-point click-count)))
  1029.         (set-marker mouse-secondary-start nil)
  1030.         (move-overlay mouse-secondary-overlay 1 1
  1031.               (window-buffer start-window))
  1032.         (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
  1033.               (window-buffer start-window)))
  1034.     ;; Single-press: cancel any preexisting secondary selection.
  1035.     (or mouse-secondary-start
  1036.         (setq mouse-secondary-start (make-marker)))
  1037.     (set-marker mouse-secondary-start start-point)
  1038.     (delete-overlay mouse-secondary-overlay))
  1039.       (let (event end end-point)
  1040.     (track-mouse
  1041.       (while (progn
  1042.            (setq event (read-event))
  1043.            (or (mouse-movement-p event)
  1044.                (eq (car-safe event) 'switch-frame)))
  1045.  
  1046.         (if (eq (car-safe event) 'switch-frame)
  1047.         nil
  1048.           (setq end (event-end event)
  1049.             end-point (posn-point end))
  1050.           (cond
  1051.            ;; Are we moving within the original window?
  1052.            ((and (eq (posn-window end) start-window)
  1053.              (integer-or-marker-p end-point))
  1054.         (let ((range (mouse-start-end start-point end-point
  1055.                           click-count)))
  1056.           (if (or (/= start-point end-point)
  1057.               (null (marker-position mouse-secondary-start)))
  1058.               (progn
  1059.             (set-marker mouse-secondary-start nil)
  1060.             (move-overlay mouse-secondary-overlay
  1061.                       (car range) (nth 1 range))))))
  1062.                (t
  1063.                 (let ((mouse-row (cdr (cdr (mouse-position)))))
  1064.                   (cond
  1065.                    ((null mouse-row))
  1066.                    ((< mouse-row top)
  1067.                     (mouse-scroll-subr start-window (- mouse-row top)
  1068.                        mouse-secondary-overlay start-point))
  1069.                    ((>= mouse-row bottom)
  1070.                     (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
  1071.                                        mouse-secondary-overlay start-point)))))))))
  1072.  
  1073.     (if (consp event)
  1074. ;;;         (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
  1075. ;;;         (eq (posn-window (event-end event)) start-window)
  1076. ;;;         (numberp (posn-point (event-end event)))
  1077.         (if (marker-position mouse-secondary-start)
  1078.         (save-window-excursion
  1079.           (delete-overlay mouse-secondary-overlay)
  1080.           (x-set-selection 'SECONDARY nil)
  1081.           (select-window start-window)
  1082.           (save-excursion
  1083.             (goto-char mouse-secondary-start)
  1084.             (sit-for 1)))
  1085.           (x-set-selection
  1086.            'SECONDARY
  1087.            (buffer-substring (overlay-start mouse-secondary-overlay)
  1088.                  (overlay-end mouse-secondary-overlay)))))))))
  1089.  
  1090. (defun mouse-yank-secondary (click)
  1091.   "Insert the secondary selection at the position clicked on.
  1092. Move point to the end of the inserted text.
  1093. If `mouse-yank-at-point' is non-nil, insert at point
  1094. regardless of where you click."
  1095.   (interactive "e")
  1096.   ;; Give temporary modes such as isearch a chance to turn off.
  1097.   (run-hooks 'mouse-leave-buffer-hook)
  1098.   (or mouse-yank-at-point (mouse-set-point click))
  1099.   (insert (x-get-selection 'SECONDARY)))
  1100.  
  1101. (defun mouse-kill-secondary ()
  1102.   "Kill the text in the secondary selection.
  1103. This is intended more as a keyboard command than as a mouse command
  1104. but it can work as either one.
  1105.  
  1106. The current buffer (in case of keyboard use), or the buffer clicked on,
  1107. must be the one that the secondary selection is in.  This requirement
  1108. is to prevent accidents."
  1109.   (interactive)
  1110.   (let* ((keys (this-command-keys))
  1111.      (click (elt keys (1- (length keys)))))
  1112.     (or (eq (overlay-buffer mouse-secondary-overlay)
  1113.         (if (listp click)
  1114.         (window-buffer (posn-window (event-start click)))
  1115.           (current-buffer)))
  1116.     (error "Select or click on the buffer where the secondary selection is")))
  1117.   (let (this-command)
  1118.     (save-excursion
  1119.       (set-buffer (overlay-buffer mouse-secondary-overlay))
  1120.       (kill-region (overlay-start mouse-secondary-overlay)
  1121.            (overlay-end mouse-secondary-overlay))))
  1122.   (delete-overlay mouse-secondary-overlay)
  1123. ;;;  (x-set-selection 'SECONDARY nil)
  1124.   (setq mouse-secondary-overlay nil))
  1125.  
  1126. (defun mouse-secondary-save-then-kill (click)
  1127.   "Save text to point in kill ring; the second time, kill the text.
  1128. You must use this in a buffer where you have recently done \\[mouse-start-secondary].
  1129. If the text between where you did \\[mouse-start-secondary] and where
  1130. you use this command matches the text at the front of the kill ring,
  1131. this command deletes the text.
  1132. Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
  1133. which prepares for a second click with this command to delete the text.
  1134.  
  1135. If you have already made a secondary selection in that buffer,
  1136. this command extends or retracts the selection to where you click.
  1137. If you do this again in a different position, it extends or retracts
  1138. again.  If you do this twice in the same position, it kills the selection."
  1139.   (interactive "e")
  1140.   (mouse-minibuffer-check click)
  1141.   (let ((posn (event-start click))
  1142.     (click-posn (posn-point (event-start click)))
  1143.     ;; Don't let a subsequent kill command append to this one:
  1144.     ;; prevent setting this-command to kill-region.
  1145.     (this-command this-command))
  1146.     (or (eq (window-buffer (posn-window posn))
  1147.         (or (and mouse-secondary-overlay
  1148.              (overlay-buffer mouse-secondary-overlay))
  1149.         (if mouse-secondary-start
  1150.             (marker-buffer mouse-secondary-start))))
  1151.     (error "Wrong buffer"))
  1152.     (save-excursion
  1153.       (set-buffer (window-buffer (posn-window posn)))
  1154.       (if (> (mod mouse-secondary-click-count 3) 0)
  1155.       (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
  1156.             (equal click-posn
  1157.                    (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
  1158.           ;; Find both ends of the object selected by this click.
  1159.           (let* ((range
  1160.               (mouse-start-end click-posn click-posn
  1161.                        mouse-secondary-click-count)))
  1162.         ;; Move whichever end is closer to the click.
  1163.         ;; That's what xterm does, and it seems reasonable.
  1164.         (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
  1165.                (abs (- click-posn (overlay-end mouse-secondary-overlay))))
  1166.             (move-overlay mouse-secondary-overlay (car range)
  1167.                   (overlay-end mouse-secondary-overlay))
  1168.           (move-overlay mouse-secondary-overlay
  1169.                 (overlay-start mouse-secondary-overlay)
  1170.                 (nth 1 range)))
  1171.         ;; We have already put the old region in the kill ring.
  1172.         ;; Replace it with the extended region.
  1173.         ;; (It would be annoying to make a separate entry.)
  1174.         (kill-new (buffer-substring
  1175.                (overlay-start mouse-secondary-overlay)
  1176.                (overlay-end mouse-secondary-overlay)) t)
  1177.         ;; Arrange for a repeated mouse-3 to kill this region.
  1178.         (setq mouse-save-then-kill-posn
  1179.               (list (car kill-ring) (point) click-posn)))
  1180.         ;; If we click this button again without moving it,
  1181.         ;; that time kill.
  1182.         (progn
  1183.           (mouse-save-then-kill-delete-region
  1184.            (overlay-start mouse-secondary-overlay)
  1185.            (overlay-end mouse-secondary-overlay))
  1186.           (setq mouse-save-then-kill-posn nil)
  1187.           (setq mouse-secondary-click-count 0)
  1188.           (delete-overlay mouse-secondary-overlay)))
  1189.     (if (and (eq last-command 'mouse-secondary-save-then-kill)
  1190.          mouse-save-then-kill-posn
  1191.          (eq (car mouse-save-then-kill-posn) (car kill-ring))
  1192.          (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
  1193.         ;; If this is the second time we've called
  1194.         ;; mouse-secondary-save-then-kill, delete the text from the buffer.
  1195.         (progn
  1196.           (mouse-save-then-kill-delete-region
  1197.            (overlay-start mouse-secondary-overlay)
  1198.            (overlay-end mouse-secondary-overlay))
  1199.           (setq mouse-save-then-kill-posn nil)
  1200.           (delete-overlay mouse-secondary-overlay))
  1201.       (if (overlay-start mouse-secondary-overlay)
  1202.           ;; We have a selection, so adjust it.
  1203.           (progn
  1204.         (if (numberp click-posn)
  1205.             (progn
  1206.               ;; Move whichever end of the region is closer to the click.
  1207.               ;; That is what xterm does, and it seems reasonable.
  1208.               (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
  1209.                  (abs (- click-posn (overlay-end mouse-secondary-overlay))))
  1210.               (move-overlay mouse-secondary-overlay click-posn
  1211.                     (overlay-end mouse-secondary-overlay))
  1212.             (move-overlay mouse-secondary-overlay
  1213.                       (overlay-start mouse-secondary-overlay)
  1214.                       click-posn))
  1215.               (setq deactivate-mark nil)))
  1216.         (if (eq last-command 'mouse-secondary-save-then-kill)
  1217.             ;; If the front of the kill ring comes from 
  1218.             ;; an immediately previous use of this command,
  1219.             ;; replace it with the extended region.
  1220.             ;; (It would be annoying to make a separate entry.)
  1221.             (kill-new (buffer-substring
  1222.                    (overlay-start mouse-secondary-overlay)
  1223.                    (overlay-end mouse-secondary-overlay)) t)
  1224.           (copy-region-as-kill (overlay-start mouse-secondary-overlay)
  1225.                        (overlay-end mouse-secondary-overlay))))
  1226.         (if mouse-secondary-start
  1227.         ;; All we have is one end of a selection,
  1228.         ;; so put the other end here.
  1229.         (let ((start (+ 0 mouse-secondary-start)))
  1230.           (kill-ring-save start click-posn)
  1231.           (if mouse-secondary-overlay
  1232.               (move-overlay mouse-secondary-overlay start click-posn)
  1233.             (setq mouse-secondary-overlay (make-overlay start click-posn)))
  1234.           (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
  1235.       (setq mouse-save-then-kill-posn
  1236.         (list (car kill-ring) (point) click-posn))))
  1237.       (if (overlay-buffer mouse-secondary-overlay)
  1238.       (x-set-selection 'SECONDARY
  1239.                (buffer-substring
  1240.                 (overlay-start mouse-secondary-overlay)
  1241.                 (overlay-end mouse-secondary-overlay)))))))
  1242.  
  1243. (defvar mouse-menu-buffer-maxlen 20
  1244.   "*Number of buffers in one pane (submenu) of the buffer menu.
  1245. If we have lots of buffers, divide them into groups of
  1246. `mouse-menu-buffer-maxlen' and make a pane (or submenu) for each one.")
  1247.  
  1248. (defun mouse-buffer-menu (event)
  1249.   "Pop up a menu of buffers for selection with the mouse.
  1250. This switches buffers in the window that you clicked on,
  1251. and selects that window."
  1252.   (interactive "e")
  1253.   (mouse-minibuffer-check event)
  1254.   (let* ((buffers
  1255.       ;; Make an alist of (MENU-ITEM . BUFFER).
  1256.       (let ((tail (buffer-list))
  1257.         (maxlen 0)
  1258.         head)
  1259.         (while tail
  1260.           (or (eq ?\ (aref (buffer-name (car tail)) 0))
  1261.           (setq maxlen
  1262.             (max maxlen
  1263.                  (length (buffer-name (car tail))))))
  1264.           (setq tail (cdr tail)))
  1265.         (setq tail (buffer-list))
  1266.         (while tail
  1267.           (let ((elt (car tail)))
  1268.         (if (/= (aref (buffer-name elt) 0) ?\ )
  1269.             (setq head
  1270.               (cons
  1271.                (cons
  1272.                 (format
  1273.                  (format "%%%ds  %%s%%s  %%s" maxlen)
  1274.                  (buffer-name elt)
  1275.                  (if (buffer-modified-p elt) "*" " ")
  1276.                  (save-excursion
  1277.                    (set-buffer elt)
  1278.                    (if buffer-read-only "%" " "))
  1279.                  (or (buffer-file-name elt) 
  1280.                  (save-excursion
  1281.                    (set-buffer elt)
  1282.                    (if list-buffers-directory
  1283.                        (expand-file-name
  1284.                     list-buffers-directory)))
  1285.                  ""))
  1286.                 elt)
  1287.                head))))
  1288.           (setq tail (cdr tail)))
  1289.         ;; Compensate for the reversal that the above loop does.
  1290.         (nreverse head)))
  1291.      (menu
  1292.       ;; If we have lots of buffers, divide them into groups of 20
  1293.       ;; and make a pane (or submenu) for each one.
  1294.       (if (> (length buffers) (/ (* mouse-menu-buffer-maxlen 3) 2))
  1295.           (let ((buffers buffers) sublists next
  1296.             (i 1))
  1297.         (while buffers
  1298.           ;; Pull off the next mouse-menu-buffer-maxlen buffers
  1299.           ;; and make them the next element of sublist.
  1300.           (setq next (nthcdr mouse-menu-buffer-maxlen buffers))
  1301.           (if next
  1302.               (setcdr (nthcdr (1- mouse-menu-buffer-maxlen) buffers)
  1303.                   nil))
  1304.           (setq sublists (cons (cons (format "Buffers %d" i) buffers)
  1305.                        sublists))
  1306.           (setq i (1+ i))
  1307.           (setq buffers next))
  1308.         (cons "Buffer Menu" (nreverse sublists)))
  1309.         ;; Few buffers--put them all in one pane.
  1310.         (list "Buffer Menu" (cons "Select Buffer" buffers)))))
  1311.     (let ((buf (x-popup-menu event menu))
  1312.       (window (posn-window (event-start event))))
  1313.       (if buf
  1314.       (progn
  1315.         (or (framep window) (select-window window))
  1316.         (switch-to-buffer buf))))))
  1317.  
  1318. ;;; These need to be rewritten for the new scroll bar implementation.
  1319.  
  1320. ;;;!! ;; Commands for the scroll bar.
  1321. ;;;!! 
  1322. ;;;!! (defun mouse-scroll-down (click)
  1323. ;;;!!   (interactive "@e")
  1324. ;;;!!   (scroll-down (1+ (cdr (mouse-coords click)))))
  1325. ;;;!! 
  1326. ;;;!! (defun mouse-scroll-up (click)
  1327. ;;;!!   (interactive "@e")
  1328. ;;;!!   (scroll-up (1+ (cdr (mouse-coords click)))))
  1329. ;;;!! 
  1330. ;;;!! (defun mouse-scroll-down-full ()
  1331. ;;;!!   (interactive "@")
  1332. ;;;!!   (scroll-down nil))
  1333. ;;;!! 
  1334. ;;;!! (defun mouse-scroll-up-full ()
  1335. ;;;!!   (interactive "@")
  1336. ;;;!!   (scroll-up nil))
  1337. ;;;!! 
  1338. ;;;!! (defun mouse-scroll-move-cursor (click)
  1339. ;;;!!   (interactive "@e")
  1340. ;;;!!   (move-to-window-line (1+ (cdr (mouse-coords click)))))
  1341. ;;;!! 
  1342. ;;;!! (defun mouse-scroll-absolute (event)
  1343. ;;;!!   (interactive "@e")
  1344. ;;;!!   (let* ((pos (car event))
  1345. ;;;!!      (position (car pos))
  1346. ;;;!!      (length (car (cdr pos))))
  1347. ;;;!!     (if (<= length 0) (setq length 1))
  1348. ;;;!!     (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
  1349. ;;;!!        (newpos (* (/ (* (/ (buffer-size) scale-factor)
  1350. ;;;!!                 position)
  1351. ;;;!!              length)
  1352. ;;;!!               scale-factor)))
  1353. ;;;!!       (goto-char newpos)
  1354. ;;;!!       (recenter '(4)))))
  1355. ;;;!! 
  1356. ;;;!! (defun mouse-scroll-left (click)
  1357. ;;;!!   (interactive "@e")
  1358. ;;;!!   (scroll-left (1+ (car (mouse-coords click)))))
  1359. ;;;!! 
  1360. ;;;!! (defun mouse-scroll-right (click)
  1361. ;;;!!   (interactive "@e")
  1362. ;;;!!   (scroll-right (1+ (car (mouse-coords click)))))
  1363. ;;;!! 
  1364. ;;;!! (defun mouse-scroll-left-full ()
  1365. ;;;!!   (interactive "@")
  1366. ;;;!!   (scroll-left nil))
  1367. ;;;!! 
  1368. ;;;!! (defun mouse-scroll-right-full ()
  1369. ;;;!!   (interactive "@")
  1370. ;;;!!   (scroll-right nil))
  1371. ;;;!! 
  1372. ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
  1373. ;;;!!   (interactive "@e")
  1374. ;;;!!   (move-to-column (1+ (car (mouse-coords click)))))
  1375. ;;;!! 
  1376. ;;;!! (defun mouse-scroll-absolute-horizontally (event)
  1377. ;;;!!   (interactive "@e")
  1378. ;;;!!   (let* ((pos (car event))
  1379. ;;;!!      (position (car pos))
  1380. ;;;!!      (length (car (cdr pos))))
  1381. ;;;!!   (set-window-hscroll (selected-window) 33)))
  1382. ;;;!! 
  1383. ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
  1384. ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
  1385. ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
  1386. ;;;!! 
  1387. ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
  1388. ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
  1389. ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
  1390. ;;;!! 
  1391. ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
  1392. ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
  1393. ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
  1394. ;;;!! 
  1395. ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
  1396. ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
  1397. ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
  1398. ;;;!! 
  1399. ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
  1400. ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
  1401. ;;;!!         'mouse-scroll-absolute-horizontally)
  1402. ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
  1403. ;;;!! 
  1404. ;;;!! (global-set-key [horizontal-slider mouse-1]
  1405. ;;;!!         'mouse-scroll-move-cursor-horizontally)
  1406. ;;;!! (global-set-key [horizontal-slider mouse-2]
  1407. ;;;!!         'mouse-scroll-move-cursor-horizontally)
  1408. ;;;!! (global-set-key [horizontal-slider mouse-3]
  1409. ;;;!!         'mouse-scroll-move-cursor-horizontally)
  1410. ;;;!! 
  1411. ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
  1412. ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
  1413. ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
  1414. ;;;!! 
  1415. ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
  1416. ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
  1417. ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
  1418. ;;;!! 
  1419. ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
  1420. ;;;!!         'mouse-split-window-horizontally)
  1421. ;;;!! (global-set-key [mode-line S-mouse-2]
  1422. ;;;!!         'mouse-split-window-horizontally)
  1423. ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
  1424. ;;;!!         'mouse-split-window)
  1425.  
  1426. ;;;!! ;;;;
  1427. ;;;!! ;;;; Here are experimental things being tested.  Mouse events
  1428. ;;;!! ;;;; are of the form:
  1429. ;;;!! ;;;;    ((x y) window screen-part key-sequence timestamp)
  1430. ;;;!! ;;
  1431. ;;;!! ;;;;
  1432. ;;;!! ;;;; Dynamically track mouse coordinates
  1433. ;;;!! ;;;;
  1434. ;;;!! ;;
  1435. ;;;!! ;;(defun track-mouse (event)
  1436. ;;;!! ;;  "Track the coordinates, absolute and relative, of the mouse."
  1437. ;;;!! ;;  (interactive "@e")
  1438. ;;;!! ;;  (while mouse-grabbed
  1439. ;;;!! ;;    (let* ((pos (read-mouse-position (selected-screen)))
  1440. ;;;!! ;;       (abs-x (car pos))
  1441. ;;;!! ;;       (abs-y (cdr pos))
  1442. ;;;!! ;;       (relative-coordinate (coordinates-in-window-p
  1443. ;;;!! ;;                 (list (car pos) (cdr pos))
  1444. ;;;!! ;;                 (selected-window))))
  1445. ;;;!! ;;      (if (consp relative-coordinate)
  1446. ;;;!! ;;      (message "mouse: [%d %d], (%d %d)" abs-x abs-y
  1447. ;;;!! ;;           (car relative-coordinate)
  1448. ;;;!! ;;           (car (cdr relative-coordinate)))
  1449. ;;;!! ;;    (message "mouse: [%d %d]" abs-x abs-y)))))
  1450. ;;;!! 
  1451. ;;;!! ;;
  1452. ;;;!! ;; Dynamically put a box around the line indicated by point
  1453. ;;;!! ;;
  1454. ;;;!! ;;
  1455. ;;;!! ;;(require 'backquote)
  1456. ;;;!! ;;
  1457. ;;;!! ;;(defun mouse-select-buffer-line (event)
  1458. ;;;!! ;;  (interactive "@e")
  1459. ;;;!! ;;  (let ((relative-coordinate
  1460. ;;;!! ;;     (coordinates-in-window-p (car event) (selected-window)))
  1461. ;;;!! ;;    (abs-y (car (cdr (car event)))))
  1462. ;;;!! ;;    (if (consp relative-coordinate)
  1463. ;;;!! ;;    (progn
  1464. ;;;!! ;;      (save-excursion
  1465. ;;;!! ;;        (move-to-window-line (car (cdr relative-coordinate)))
  1466. ;;;!! ;;        (x-draw-rectangle
  1467. ;;;!! ;;         (selected-screen)
  1468. ;;;!! ;;         abs-y 0
  1469. ;;;!! ;;         (save-excursion
  1470. ;;;!! ;;         (move-to-window-line (car (cdr relative-coordinate)))
  1471. ;;;!! ;;         (end-of-line)
  1472. ;;;!! ;;         (push-mark nil t)
  1473. ;;;!! ;;         (beginning-of-line)
  1474. ;;;!! ;;         (- (region-end) (region-beginning))) 1))
  1475. ;;;!! ;;      (sit-for 1)
  1476. ;;;!! ;;      (x-erase-rectangle (selected-screen))))))
  1477. ;;;!! ;;
  1478. ;;;!! ;;(defvar last-line-drawn nil)
  1479. ;;;!! ;;(defvar begin-delim "[^ \t]")
  1480. ;;;!! ;;(defvar end-delim   "[^ \t]")
  1481. ;;;!! ;;
  1482. ;;;!! ;;(defun mouse-boxing (event)
  1483. ;;;!! ;;  (interactive "@e")
  1484. ;;;!! ;;  (save-excursion
  1485. ;;;!! ;;    (let ((screen (selected-screen)))
  1486. ;;;!! ;;      (while (= (x-mouse-events) 0)
  1487. ;;;!! ;;    (let* ((pos (read-mouse-position screen))
  1488. ;;;!! ;;           (abs-x (car pos))
  1489. ;;;!! ;;           (abs-y (cdr pos))
  1490. ;;;!! ;;           (relative-coordinate
  1491. ;;;!! ;;        (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
  1492. ;;;!! ;;                     (selected-window)))
  1493. ;;;!! ;;           (begin-reg nil)
  1494. ;;;!! ;;           (end-reg nil)
  1495. ;;;!! ;;           (end-column nil)
  1496. ;;;!! ;;           (begin-column nil))
  1497. ;;;!! ;;      (if (and (consp relative-coordinate)
  1498. ;;;!! ;;           (or (not last-line-drawn)
  1499. ;;;!! ;;               (not (= last-line-drawn abs-y))))
  1500. ;;;!! ;;          (progn
  1501. ;;;!! ;;        (move-to-window-line (car (cdr relative-coordinate)))
  1502. ;;;!! ;;        (if (= (following-char) 10)
  1503. ;;;!! ;;            ()
  1504. ;;;!! ;;          (progn
  1505. ;;;!! ;;            (setq begin-reg (1- (re-search-forward end-delim)))
  1506. ;;;!! ;;            (setq begin-column (1- (current-column)))
  1507. ;;;!! ;;            (end-of-line)
  1508. ;;;!! ;;            (setq end-reg (1+ (re-search-backward begin-delim)))
  1509. ;;;!! ;;            (setq end-column (1+ (current-column)))
  1510. ;;;!! ;;            (message "%s" (buffer-substring begin-reg end-reg))
  1511. ;;;!! ;;            (x-draw-rectangle screen
  1512. ;;;!! ;;                      (setq last-line-drawn abs-y)
  1513. ;;;!! ;;                      begin-column
  1514. ;;;!! ;;                      (- end-column begin-column) 1))))))))))
  1515. ;;;!! ;;
  1516. ;;;!! ;;(defun mouse-erase-box ()
  1517. ;;;!! ;;  (interactive)
  1518. ;;;!! ;;  (if last-line-drawn
  1519. ;;;!! ;;      (progn
  1520. ;;;!! ;;    (x-erase-rectangle (selected-screen))
  1521. ;;;!! ;;    (setq last-line-drawn nil))))
  1522. ;;;!! 
  1523. ;;;!! ;;; (defun test-x-rectangle ()
  1524. ;;;!! ;;;   (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
  1525. ;;;!! ;;;   (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
  1526. ;;;!! ;;;   (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
  1527. ;;;!! 
  1528. ;;;!! ;;
  1529. ;;;!! ;; Here is how to do double clicking in lisp.  About to change.
  1530. ;;;!! ;;
  1531. ;;;!! 
  1532. ;;;!! (defvar double-start nil)
  1533. ;;;!! (defconst double-click-interval 300
  1534. ;;;!!   "Max ticks between clicks")
  1535. ;;;!! 
  1536. ;;;!! (defun double-down (event)
  1537. ;;;!!   (interactive "@e")
  1538. ;;;!!   (if double-start
  1539. ;;;!!       (let ((interval (- (nth 4 event) double-start)))
  1540. ;;;!!     (if (< interval double-click-interval)
  1541. ;;;!!         (progn
  1542. ;;;!!           (backward-up-list 1)
  1543. ;;;!!           ;;      (message "Interval %d" interval)
  1544. ;;;!!           (sleep-for 1)))
  1545. ;;;!!     (setq double-start nil))
  1546. ;;;!!     (setq double-start (nth 4 event))))
  1547. ;;;!!     
  1548. ;;;!! (defun double-up (event)
  1549. ;;;!!   (interactive "@e")
  1550. ;;;!!   (and double-start
  1551. ;;;!!        (> (- (nth 4 event ) double-start) double-click-interval)
  1552. ;;;!!        (setq double-start nil)))
  1553. ;;;!! 
  1554. ;;;!! ;;; (defun x-test-doubleclick ()
  1555. ;;;!! ;;;   (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
  1556. ;;;!! ;;;   (define-key doubleclick-test-map mouse-button-left 'double-down)
  1557. ;;;!! ;;;   (define-key doubleclick-test-map mouse-button-left-up 'double-up))
  1558. ;;;!! 
  1559. ;;;!! ;;
  1560. ;;;!! ;; This scrolls while button is depressed.  Use preferable in scroll bar.
  1561. ;;;!! ;;
  1562. ;;;!! 
  1563. ;;;!! (defvar scrolled-lines 0)
  1564. ;;;!! (defconst scroll-speed 1)
  1565. ;;;!! 
  1566. ;;;!! (defun incr-scroll-down (event)
  1567. ;;;!!   (interactive "@e")
  1568. ;;;!!   (setq scrolled-lines 0)
  1569. ;;;!!   (incremental-scroll scroll-speed))
  1570. ;;;!! 
  1571. ;;;!! (defun incr-scroll-up (event)
  1572. ;;;!!   (interactive "@e")
  1573. ;;;!!   (setq scrolled-lines 0)
  1574. ;;;!!   (incremental-scroll (- scroll-speed)))
  1575. ;;;!! 
  1576. ;;;!! (defun incremental-scroll (n)
  1577. ;;;!!   (while (= (x-mouse-events) 0)
  1578. ;;;!!     (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
  1579. ;;;!!     (scroll-down n)
  1580. ;;;!!     (sit-for 300 t)))
  1581. ;;;!! 
  1582. ;;;!! (defun incr-scroll-stop (event)
  1583. ;;;!!   (interactive "@e")
  1584. ;;;!!   (message "Scrolled %d lines" scrolled-lines)
  1585. ;;;!!   (setq scrolled-lines 0)
  1586. ;;;!!   (sleep-for 1))
  1587. ;;;!! 
  1588. ;;;!! ;;; (defun x-testing-scroll ()
  1589. ;;;!! ;;;   (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
  1590. ;;;!! ;;;     (define-key scrolling-map mouse-button-left 'incr-scroll-down)
  1591. ;;;!! ;;;     (define-key scrolling-map mouse-button-right 'incr-scroll-up)
  1592. ;;;!! ;;;     (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
  1593. ;;;!! ;;;     (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
  1594. ;;;!! 
  1595. ;;;!! ;;
  1596. ;;;!! ;; Some playthings suitable for picture mode?  They need work.
  1597. ;;;!! ;;
  1598. ;;;!! 
  1599. ;;;!! (defun mouse-kill-rectangle (event)
  1600. ;;;!!   "Kill the rectangle between point and the mouse cursor."
  1601. ;;;!!   (interactive "@e")
  1602. ;;;!!   (let ((point-save (point)))
  1603. ;;;!!     (save-excursion
  1604. ;;;!!       (mouse-set-point event)
  1605. ;;;!!       (push-mark nil t)
  1606. ;;;!!       (if (> point-save (point))
  1607. ;;;!!       (kill-rectangle (point) point-save)
  1608. ;;;!!     (kill-rectangle point-save (point))))))
  1609. ;;;!! 
  1610. ;;;!! (defun mouse-open-rectangle (event)
  1611. ;;;!!   "Kill the rectangle between point and the mouse cursor."
  1612. ;;;!!   (interactive "@e")
  1613. ;;;!!   (let ((point-save (point)))
  1614. ;;;!!     (save-excursion
  1615. ;;;!!       (mouse-set-point event)
  1616. ;;;!!       (push-mark nil t)
  1617. ;;;!!       (if (> point-save (point))
  1618. ;;;!!       (open-rectangle (point) point-save)
  1619. ;;;!!     (open-rectangle point-save (point))))))
  1620. ;;;!! 
  1621. ;;;!! ;; Must be a better way to do this.
  1622. ;;;!! 
  1623. ;;;!! (defun mouse-multiple-insert (n char)
  1624. ;;;!!   (while (> n 0)
  1625. ;;;!!     (insert char)
  1626. ;;;!!     (setq n (1- n))))
  1627. ;;;!! 
  1628. ;;;!! ;; What this could do is not finalize until button was released.
  1629. ;;;!! 
  1630. ;;;!! (defun mouse-move-text (event)
  1631. ;;;!!   "Move text from point to cursor position, inserting spaces."
  1632. ;;;!!   (interactive "@e")
  1633. ;;;!!   (let* ((relative-coordinate
  1634. ;;;!!       (coordinates-in-window-p (car event) (selected-window))))
  1635. ;;;!!     (if (consp relative-coordinate)
  1636. ;;;!!     (cond ((> (current-column) (car relative-coordinate))
  1637. ;;;!!            (delete-char
  1638. ;;;!!         (- (car relative-coordinate) (current-column))))
  1639. ;;;!!           ((< (current-column) (car relative-coordinate))
  1640. ;;;!!            (mouse-multiple-insert
  1641. ;;;!!         (- (car relative-coordinate) (current-column)) " "))
  1642. ;;;!!           ((= (current-column) (car relative-coordinate)) (ding))))))
  1643.  
  1644. ;; Choose a completion with the mouse.
  1645.  
  1646. (defun mouse-choose-completion (event)
  1647.   "Click on an alternative in the `*Completions*' buffer to choose it."
  1648.   (interactive "e")
  1649.   ;; Give temporary modes such as isearch a chance to turn off.
  1650.   (run-hooks 'mouse-leave-buffer-hook)
  1651.   (let ((buffer (window-buffer))
  1652.         choice
  1653.     base-size)
  1654.     (save-excursion
  1655.       (set-buffer (window-buffer (posn-window (event-start event))))
  1656.       (if completion-reference-buffer
  1657.       (setq buffer completion-reference-buffer))
  1658.       (setq base-size completion-base-size)
  1659.       (save-excursion
  1660.     (goto-char (posn-point (event-start event)))
  1661.     (let (beg end)
  1662.       (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
  1663.           (setq end (point) beg (1+ (point))))
  1664.       (if (null beg)
  1665.           (error "No completion here"))
  1666.       (setq beg (previous-single-property-change beg 'mouse-face))
  1667.       (setq end (or (next-single-property-change end 'mouse-face)
  1668.             (point-max)))
  1669.       (setq choice (buffer-substring beg end)))))
  1670.     (let ((owindow (selected-window)))
  1671.       (select-window (posn-window (event-start event)))
  1672.       (if (and (one-window-p t 'selected-frame)
  1673.            (window-dedicated-p (selected-window)))
  1674.       ;; This is a special buffer's frame
  1675.       (iconify-frame (selected-frame))
  1676.     (or (window-dedicated-p (selected-window))
  1677.         (bury-buffer)))
  1678.       (select-window owindow))
  1679.     (choose-completion-string choice buffer base-size)))
  1680.  
  1681. ;; Font selection.
  1682.  
  1683. (defun font-menu-add-default ()
  1684.   (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
  1685.      (font-alist x-fixed-font-alist)
  1686.      (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
  1687.     (if (assoc "Default" elt)
  1688.     (delete (assoc "Default" elt) elt))
  1689.     (setcdr elt
  1690.         (cons (list "Default"
  1691.             (cdr (assq 'font (frame-parameters (selected-frame)))))
  1692.           (cdr elt)))))
  1693.  
  1694. (defvar x-fixed-font-alist
  1695.   '("Font menu"
  1696.     ("Misc"
  1697.      ;; For these, we specify the pixel height and width.
  1698.      ("fixed" "fixed")
  1699.      ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
  1700.      ("6x12"
  1701.       "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
  1702.      ("6x13"
  1703.       "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
  1704.      ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
  1705.      ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
  1706.      ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
  1707.      ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
  1708.      ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
  1709.      ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
  1710.      ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
  1711.      ("")
  1712.      ("clean 5x8"
  1713.       "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
  1714.      ("clean 6x8"
  1715.       "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
  1716.      ("clean 8x8"
  1717.       "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
  1718.      ("clean 8x10"
  1719.       "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
  1720.      ("clean 8x14"
  1721.       "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
  1722.      ("clean 8x16"
  1723.       "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
  1724.      ("")
  1725.      ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1"))
  1726. ;;; We don't seem to have these; who knows what they are.
  1727. ;;;    ("fg-18" "fg-18")
  1728. ;;;    ("fg-25" "fg-25")
  1729. ;;;    ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
  1730. ;;;    ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
  1731. ;;;    ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
  1732. ;;;    ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
  1733. ;;;    ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
  1734.     ("Courier"
  1735.      ;; For these, we specify the point height.
  1736.      ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
  1737.      ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
  1738.      ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
  1739.      ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
  1740.      ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
  1741.      ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
  1742.      ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
  1743.      ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
  1744.      ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
  1745.      ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
  1746.      ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
  1747.      ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
  1748.      ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
  1749.      ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
  1750.      ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
  1751.      ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
  1752.      ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
  1753.      ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
  1754.      ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
  1755.      ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
  1756.      ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
  1757.      ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
  1758.      ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
  1759.      ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
  1760.     )
  1761.   "X fonts suitable for use in Emacs.")
  1762.  
  1763. (defun mouse-set-font (&rest fonts)
  1764.   "Select an emacs font from a list of known good fonts"
  1765.   (interactive
  1766.    (x-popup-menu last-nonmenu-event x-fixed-font-alist))
  1767.   (if fonts
  1768.       (let (font)
  1769.     (while fonts
  1770.       (condition-case nil
  1771.           (progn
  1772.         (set-default-font (car fonts))
  1773.         (setq font (car fonts))
  1774.         (setq fonts nil))
  1775.         (error
  1776.          (setq fonts (cdr fonts)))))
  1777.     (if (null font)
  1778.         (error "Font not found")))))
  1779.  
  1780. ;;; Bindings for mouse commands.
  1781.  
  1782. (define-key global-map [down-mouse-1] 'mouse-drag-region)
  1783. (global-set-key [mouse-1]    'mouse-set-point)
  1784. (global-set-key [drag-mouse-1]    'mouse-set-region)
  1785.  
  1786. ;; These are tested for in mouse-drag-region.
  1787. (global-set-key [double-mouse-1] 'mouse-set-point)
  1788. (global-set-key [triple-mouse-1] 'mouse-set-point)
  1789.  
  1790. (global-set-key [mouse-2]    'mouse-yank-at-click)
  1791. (global-set-key [mouse-3]    'mouse-save-then-kill)
  1792.  
  1793. ;; By binding these to down-going events, we let the user use the up-going
  1794. ;; event to make the selection, saving a click.
  1795. (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
  1796. (if (not (eq system-type 'ms-dos))
  1797.     (global-set-key [S-down-mouse-1] 'mouse-set-font))
  1798. ;; C-down-mouse-2 is bound in facemenu.el.
  1799. (global-set-key [C-down-mouse-3] 'mouse-major-mode-menu)
  1800.  
  1801.  
  1802. ;; Replaced with dragging mouse-1
  1803. ;; (global-set-key [S-mouse-1]    'mouse-set-mark)
  1804.  
  1805. (global-set-key [mode-line mouse-1] 'mouse-select-window)
  1806. (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
  1807. (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
  1808. (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
  1809. (global-set-key [mode-line mouse-3] 'mouse-delete-window)
  1810. (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
  1811. (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
  1812. (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
  1813. (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
  1814. (global-set-key [vertical-line mouse-1] 'mouse-select-window)
  1815.  
  1816. (provide 'mouse)
  1817.  
  1818. ;;; mouse.el ends here
  1819.